<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring AOP 横切编程指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        
        code {
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .annotation { color: #c586c0; }
        .keyword { color: #569cd6; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .method { color: #dcdcaa; }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-divider {
            position: relative;
            text-align: center;
            margin: 4rem 0;
        }
        
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e0e0e0, transparent);
        }
        
        .section-divider i {
            background: white;
            padding: 0 1rem;
            position: relative;
            color: #667eea;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .step-number {
            width: 32px;
            height: 32px;
            background: #667eea;
            color: white;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            margin-right: 0.5rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Spring AOP 横切编程
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    掌握面向切面编程，优雅实现横切关注点
                </p>
                <div class="flex justify-center gap-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-code mr-2"></i>代码解耦
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-layer-group mr-2"></i>横切逻辑
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-magic mr-2"></i>注解驱动
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Spring框架中，AOP（Aspect-Oriented Programming）提供了一种优雅的方式来处理横切关注点。通过切面（Aspect）对Controller的参数进行横切，我们可以实现诸如日志记录、权限验证、参数校验等功能，而无需在每个方法中重复编写相同的代码。
            </p>
        </div>

        <!-- AOP Concept Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>AOP 核心概念关系图
            </h3>
            <div class="mermaid">
                graph TD
                    A[切面 Aspect] --> B[切入点 Pointcut]
                    A --> C[通知 Advice]
                    B --> D[连接点 JoinPoint]
                    C --> E[@Before 前置通知]
                    C --> F[@After 后置通知]
                    C --> G[@Around 环绕通知]
                    C --> H[@AfterReturning 返回通知]
                    C --> I[@AfterThrowing 异常通知]
                    D --> J[方法执行]
                    style A fill:#667eea,stroke:#fff,color:#fff
                    style C fill:#764ba2,stroke:#fff,color:#fff
            </div>
        </div>

        <!-- Section 1: Basic AOP -->
        <div class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-cut mr-3 text-purple-600"></i>通过AOP注解实现横切
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-crosshairs"></i>
                    </div>
                    <h4 class="text-xl font-semibold mb-3">精准拦截</h4>
                    <p class="text-gray-600">通过切入点表达式精确定位需要横切的方法，实现细粒度的控制。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h4 class="text-xl font-semibold mb-3">参数处理</h4>
                    <p class="text-gray-600">在方法执行前后对参数进行统一处理，如数据转换、验证等。</p>
                </div>
            </div>

            <!-- Code Example 1 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <span class="step-number">1</span>定义切面类
                </h3>
                
                <div class="code-block">
                    <div class="code-header">
                        <span class="code-dot bg-red-500"></span>
                        <span class="code-dot bg-yellow-500"></span>
                        <span class="code-dot bg-green-500"></span>
                        <span class="text-gray-400 text-sm ml-2">ParameterAspect.java</span>
                    </div>
                    <pre><code><span class="annotation">@Aspect</span>
<span class="annotation">@Component</span>
<span class="keyword">public class</span> <span class="method">ParameterAspect</span> {

    <span class="annotation">@Before</span>(<span class="string">"execution(* com.ts.demo.controller.MyController.*(..))"</span>)
    <span class="keyword">public void</span> <span class="method">beforeControllerMethod</span>(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        <span class="keyword">for</span> (Object arg : args) {
            <span class="comment">// 对参数进行处理</span>
            <span class="keyword">if</span> (arg <span class="keyword">instanceof</span> String) {
                String newArg = ((String) arg).toUpperCase();
                System.out.println(<span class="string">"Original arg: "</span> + arg + <span class="string">", Processed arg: "</span> + newArg);
            }
        }
    }
}</code></pre>
                </div>
            </div>

            <!-- Implementation Steps -->
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-8">
                <h3 class="text-xl font-semibold mb-6 text-gray-800">
                    <i class="fas fa-tasks mr-2 text-purple-600"></i>实现步骤详解
                </h3>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <span class="step-number flex-shrink-0">2</span>
                        <p class="text-gray-700">在切面类上使用 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">@Aspect</code> 注解，表示该类是一个切面类，并使用 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">@Component</code> 注解将其交由Spring容器管理。</p>
                    </div>
                    
                    <div class="flex items-start">
                        <span class="step-number flex-shrink-0">3</span>
                        <p class="text-gray-700">在 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">beforeControllerMethod</code> 方法上使用 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">@Before</code> 注解，指定切入点表达式。</p>
                    </div>
                    
                    <div class="flex items-start">
                        <span class="step-number flex-shrink-0">4</span>
                        <p class="text-gray-700">在通知方法中使用 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">JoinPoint</code> 来获取方法的参数，并进行相应的处理。</p>
                    </div>
                </div>
            </div>

            <!-- Controller Example -->
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <span class="step-number">5</span>Controller 实现
                </h3>
                
                <div class="code-block">
                    <div class="code-header">
                        <span class="code-dot bg-red-500"></span>
                        <span class="code-dot bg-yellow-500"></span>
                        <span class="code-dot bg-green-500"></span>
                        <span class="text-gray-400 text-sm ml-2">MyController.java</span>
                    </div>
                    <pre><code><span class="annotation">@RestController</span>
<span class="keyword">public class</span> <span class="method">MyController</span> {

    <span class="annotation">@GetMapping</span>(<span class="string">"/hello"</span>)
    <span class="keyword">public</span> String <span class="method">sayHello</span>(<span class="annotation">@RequestParam</span>(<span class="string">"name"</span>) String name) {
        <span class="keyword">return</span> <span class="string">"Hello, "</span> + name + <span class="string">"!"</span>;
    }
}</code></pre>
                </div>
                
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-gray-700">
                        <i class="fas fa-info-circle text-purple-600 mr-2"></i>
                        当请求 <code class="bg-white px-2 py-1 rounded text-purple-600">/hello?name=John</code> 时，AOP切面会对 <code class="bg-white px-2 py-1 rounded text-purple-600">name</code> 参数进行横切，并将其转换成大写后输